package com.o2o.utils.baidu;

/**
 * package_name:com.o2o.utils.baidu
 * Author:徐亚远
 * Date:2019/12/24 12:18
 * 项目名:o2o
 * Desription:
 **/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class ShortNetAddress {
    private static Logger log = LoggerFactory.getLogger(ShortNetAddress.class);

    public static int TIMEOUT = 30 * 1000;
    public static String ENCODING = "UTF-8";

    /**
     * JSON get value by key
     *
     * @param replyText
     * @param key
     * @return
     */
    private static String getValueByKey_JSON(String replyText, String key) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        String tinyUrl = null;
        try {
            node = mapper.readTree(replyText);
            tinyUrl = node.get(key).asText();
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            log.error("getValueByKey_JSON error:" + e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("getValueByKey_JSON error:" + e.toString());
        }

        return tinyUrl;
    }

    /**
     * 通过HttpConnection 获取返回的字符串
     *
     * @param connection
     * @return
     * @throws IOException
     */
    private static String getResponseStr(HttpURLConnection connection)
            throws IOException {
        StringBuffer result = new StringBuffer();
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    in, ENCODING));
            String inputLine = "";
            while ((inputLine = reader.readLine()) != null) {
                result.append(inputLine);
            }
        }
        return String.valueOf(result);
    }

    public static String getShortURL(String originURL) {
        String tinyUrl = null;
        try {
            URL url = new URL("http://dwz.cn/create.php");
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            // POST Request Define:
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setConnectTimeout(TIMEOUT);
            connection.setRequestMethod("POST");
            String postData = URLEncoder.encode(originURL.toString(), "utf-8");
            connection.getOutputStream().write(("url=" + postData).getBytes());
            connection.connect();
            String responseStr = getResponseStr(connection);
            log.info("response string: " + responseStr);
            tinyUrl = getValueByKey_JSON(responseStr, "tinyurl");
            log.info("tinyurl: " + tinyUrl);
            connection.disconnect();
        } catch (IOException e) {
            log.error("getshortURL error:" + e.toString());
        }
        return tinyUrl;

    }

    /**
     * ‘ 百度短链接接口 无法处理不知名网站，会安全识别报错
     *
     * @param args
     */
    public static void main(String[] args) {
        getShortURL("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2c605206217d88b5&redirect_uri=http://115.28.159.6/cityrun/wechatlogin.action&role_type=1&response_type=code&scope=snsapi_userinfo&state=STATE123qweasd#wechat_redirect");
    }
}

